#include <Windows.h>
#include <SDL.h>
#include <gl/GL.h>
#include <gl/GLU.h>
#include "Terrain.h"
#undef main
Terrain* terrain;
float angle = 0;

void render()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glLoadIdentity();

	gluLookAt(0, 10, 50, 0, 0, 0, 0, 1, 0);

	glRotatef(angle, 0, 1, 0);
	angle++;
	if (angle > 360) angle -= 360.0;

	/*GLfloat ambientColor[] = {1.f, 1.f, 1.f, 1.0f};
	glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

	GLfloat lightColor0[] = {0.6f, 0.6f, 0.6f, 1.0f};
	GLfloat lightPos0[] = {2.f, 2.f, 0, 0.0f};
	glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);*/

	//float pos[]={0,5,0};
	//glLightfv(GL_LIGHT0,GL_POSITION,pos);

	terrain->draw(20.f);
	/*glBegin(GL_QUADS);
	glColor3f(1.f, 0, 0);
	glNormal3f(0, 0, 1.f);
	glVertex3f(-1, -1, 0);
	glVertex3f( 1, -1, 0);
	glVertex3f( 1,  1, 0);
	glVertex3f(-1,  1, 0);
	glEnd();*/

}

void init()
{
	glClearColor(0.0, 0.0, 0.0, 1.0);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45, 480.0/240.0, 1.0, 500.0);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
}

int main()
{
	SDL_Init( SDL_INIT_EVERYTHING );
	SDL_Surface * screen = SDL_SetVideoMode(480, 240, 32, SDL_SWSURFACE | SDL_OPENGL);

	init();
	terrain = new Terrain("test3.png");
	
	bool running = true;
	Uint32 start;
	SDL_Event evt;

	while (running)
	{
		start = SDL_GetTicks();
		while (SDL_PollEvent(&evt))
		{
			switch(evt.type)
			{
			case SDL_QUIT:
				running = false;
				break;
			case SDL_KEYDOWN:
				if (evt.key.keysym.sym == SDLK_ESCAPE) {
					running = false;
				}
				break;
			}
		}

		render();
		SDL_GL_SwapBuffers();

		if (1000/30 > (SDL_GetTicks() - start))
			SDL_Delay(1000/30 - (SDL_GetTicks() - start));
	}

	SDL_Quit();
	return 0;
}
